define(function(require,exports,module){
    var $ = require('$');
    module.exports = function( options ){
        var options = options.jointly==true ? options : $.extend( {}, options ),
            tid1    = null,tid2 = null,
            Private = {
            boxW: 0,
            curNum: 0,
            speed: 0,
            checkOption: function(){
                if( options.pageContainer && options.child ){
                    options.pageContainer = $( options.pageContainer );
                    options.child = options.pageContainer.find( options.child );
                    options.curPage = options.curPage || 1;
                    options.curZindex = options.curZindex || 100;
                    options.otherZindex = ~~options.otherZindex;
                    options.way = options.way||'scroll';
                    this.curNum = options.curPage - 1;
                    this.boxW = options.pageContainer.outerWidth( );
                }else{
                    alert( '请传入pageContainer,child两个必填参数!' );
                    return false;
                }
            },
            eleInit: function(){
                options.pageContainer.css( {'overflow-x':'hidden',position:'relative'} );
                options.child.css( {position:'absolute',top:0,width:'100%',zIndex: options.otherZindex} )
                .eq( this.curNum ).css({zIndex: options.curZindex,left:0}).fadeIn('slow').prevAll( options.child.selector||'.page' ).css({left: this.boxW*-1, display:'none'}).end()
                .nextAll( options.child.selector||'.page' ).css( {left: this.boxW, display:'none'} );
                
                this.speed = getComputedStyle( options.child[0] )['transition-duration'];
                this.speed = this.speed.indexOf('.')==0 ? '0'+this.speed : this.speed;
                this.speed = this.speed.indexOf('ms')>-1 ? parseFloat(this.speed) : parseFloat(this.speed) * 1000;
                
            },
            reSetPos: function( ele ){
                ( ele||options.child.eq( this.curNum ) ).prevAll( options.child.selector||'.page' ).css( {zIndex: options.otherZindex, left: this.boxW*-1, display:'none'} ).end()
                .nextAll( options.child.selector||'.page' ).css( {zIndex: options.otherZindex, left: this.boxW, display:'none'} );
            },
            goTo: function( cur ){
                cur = Math.min( Math.max( 1, cur ), options.child.length );
                cur -= 1;
                var lastPage = this.curNum, // 上一页的页码
                    special  = {inverse:false}; // 哎，特殊处理，针对原本从左至右的或者原本从右至左的动画，反过来运动
                    
                // 预加载事件
                'function'===typeof options.beforeLoad&&options.beforeLoad.apply( options.child.eq( cur ), [options.child.eq( cur ), lastPage, this.curNum != cur, special] );
                
                if( this.curNum != cur ){
                    
                    // 两种动画模式
                    if( options.way=='scroll' ){
                        
                        options.child.eq( this.curNum ).css( {left: this.curNum > cur ? this.boxW*(special.inverse==true?-1:1) : this.boxW*(special.inverse==true?1:-1) } );
                        
                    }else{
                        
                        options.child.eq( this.curNum ).css('zIndex', options.curZindex-1);
                        
                    }
                    
                    // 为了做过度动画，才写个css block
                    options.child.eq( this.curNum = cur ).css( special.inverse==true ? {display:'block',left: parseFloat(options.child.eq(cur).css('left'))*-1,transitionDuration:'0s'} : 'display','block' );
                    
                    clearTimeout( tid1 );
                    clearTimeout( tid2 );
                    
                    tid1 = setTimeout(function(){
                        options.child.eq( this.curNum ).css( {transitionDuration:'',zIndex: options.curZindex, left: 0} );
                    }.bind(this), 20);
                    // 动画每帧完之后所做的事务
                    tid2 = setTimeout(function(){
                        this.reSetPos( );
                        'function'===typeof options.pageLoad && options.pageLoad.apply( options.child.eq( cur ), [options.child.eq( cur ), lastPage] );
                    }.bind(this), this.speed + 20);
                }else{
                    'function'===typeof options.samePage && options.samePage.apply( options.child.eq( cur ), [options.child.eq( cur ), lastPage] );
                }
                
            },
            init: function(){
                this.checkOption();
                this.eleInit();
            }
        }
        
        if( !(this instanceof arguments.callee ) ){
            return new arguments.callee( options );
        }
        
        Private.init();
        
        this.goTo = function( curPage ){
            Private.goTo( curPage )
        }
        
    };
})